home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / irit5 / cagd_lib / cbspeval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-31  |  8.3 KB  |  196 lines

  1. /******************************************************************************
  2. * CBspEval.c - Bezier curves handling routines - evaluation routines.          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Mar. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "cagd_loc.h"
  11.  
  12. /*****************************************************************************
  13. * DESCRIPTION:                                                               M
  14. * Assumes Vec holds control points for scalar Bspline curve of order Order   M
  15. * length Len and knot vector KnotVector.                     M
  16. *   Evaluates and returns that curve value at parameter value t.         M
  17. *   Vec is incremented by VecInc (usually by 1) after each iteration.         M
  18. *                                                                            *
  19. * PARAMETERS:                                                                M
  20. *   Vec:            Coefficents of a scalar Bspline univariate function.     M
  21. *   VecInc:         Step to move along Vec.                                  M
  22. *   KnotVector:     Knot vector of associated geoemtry.                      M
  23. *   Order:          Order of associated geometry.                            M
  24. *   Len:            Length of control vector.                                M
  25. *   Periodic:       If this geometry is Periodic.                            M
  26. *   t:              Parameter value where to evaluate the curve.             M
  27. *                                                                            *
  28. * RETURN VALUE:                                                              M
  29. *   CagdRType:      Geometry's value at parameter value t.                   M
  30. *                                                                            *
  31. * KEYWORDS:                                                                  M
  32. *   BspCrvEvalVecAtParam, evaluation                                         M
  33. *****************************************************************************/
  34. CagdRType BspCrvEvalVecAtParam(CagdRType *Vec,
  35.                    int VecInc,
  36.                    CagdRType *KnotVector,
  37.                    int Order,
  38.                    int Len,
  39.                    CagdBType Periodic,
  40.                    CagdRType t)
  41. {
  42.     int i, IndexFirst;
  43.     CagdRType
  44.     R = 0.0,
  45.     *BasisFunc = BspCrvCoxDeBoorBasis(KnotVector, Order,
  46.                       Len + (Periodic ? Order - 1 : 0),
  47.                       t, &IndexFirst);
  48.  
  49.     if (VecInc == 1) {
  50.     for (i = 0; i < Order; i++)
  51.         R += BasisFunc[i] * Vec[IndexFirst++ % Len];
  52.     }
  53.     else {
  54.     int IndexFirstInc = IndexFirst;
  55.  
  56.     IndexFirstInc *= VecInc;
  57.     for (i = 0; i < Order; i++) {
  58.         R += BasisFunc[i] * Vec[IndexFirstInc];
  59.         IndexFirstInc += VecInc;
  60.         if (++IndexFirst >= Len) {
  61.             IndexFirst -= Len;
  62.         IndexFirstInc -= Len * VecInc;
  63.         }
  64.     }
  65.     }
  66.  
  67.     return R;
  68. }
  69.  
  70. /*****************************************************************************
  71. * DESCRIPTION:                                                               M
  72. * Returns a pointer to a static data, holding the value of the curve at      M
  73. * given parametric location t. The curve is assumed to be Bspline.         M
  74. *   Uses the Cox de Boor recursive algorithm.                                M
  75. *                                                                            *
  76. * PARAMETERS:                                                                M
  77. *   Crv:      To evaluate at the given parametric location t.                M
  78. *   t:        The parameter value at which the curve Crv is to be evaluated. M
  79. *                                                                            *
  80. * RETURN VALUE:                                                              M
  81. *   CagdRType *:  A vector holding all the coefficients of all components    M
  82. *                 of curve Crv's point type. If for example the curve's      M
  83. *                 point type is P2, the W, X, and Y will be saved in the     M
  84. *                 first three locations of the returned vector. The first    M
  85. *                 location (index 0) of the returned vector is reserved for  M
  86. *                 the rational coefficient W and XYZ always starts at second M
  87. *                 location of the returned vector (index 1).                 M
  88. *                                                                            *
  89. * KEYWORDS:                                                                  M
  90. *   BspCrvEvalAtParam, evaluation                                            M
  91. *****************************************************************************/
  92. CagdRType *BspCrvEvalAtParam(CagdCrvStruct *Crv, CagdRType t)
  93. {
  94.     return BspCrvEvalCoxDeBoor(Crv, t);
  95. }
  96.  
  97. /*****************************************************************************
  98. * DESCRIPTION:                                                               M
  99. * Samples the curve at FineNess location equally spaced in the curve's       M
  100. * parametric domain.                                 M
  101. *   Computes a refinement alpha matrix (If FineNess > 0), refines the curve  M
  102. * and uses refined control polygon as the approximation to the curve.         M
  103. *   If FineNess == 0, Alpha matrix A is used instead.                 M
  104. *   Returns the actual number of points in polyline (<= FineNess).         M
  105. * Note this routine may be invoked with Bezier curves as well as Bspline.    M
  106. *                                                                            *
  107. * PARAMETERS:                                                                M
  108. *   Crv:         To approximate as a polyline.                               M
  109. *   FineNess:    Control on number of samples.                               M
  110. *   Points:      Where to put the resulting polyline.                        M
  111. *   A:           Optional alpha matrix for refinement.                       M
  112. *   OptiLin:     If TRUE, optimize linear curves.                 M
  113. *                                                                            *
  114. * RETURN VALUE:                                                              M
  115. *   int:         The actual number of samples placed in Points. Always       M
  116. *                less than or eaul to FineNess.                              M
  117. *                                                                            *
  118. * KEYWORDS:                                                                  M
  119. *   CagdCrvEvalToPolyline, conversion, refinement, evaluation                M
  120. *****************************************************************************/
  121. int CagdCrvEvalToPolyline(CagdCrvStruct *Crv,
  122.               int FineNess,
  123.               CagdRType *Points[],
  124.               BspKnotAlphaCoeffType *A,
  125.               CagdBType OptiLin)
  126. {
  127.     CagdBType
  128.     IsNotRational = !CAGD_IS_RATIONAL_CRV(Crv);
  129.     int i, j, Count,
  130.     Len = Crv -> Length,
  131.         n = FineNess == 0 ? A -> RefLength : FineNess,
  132.     Order = Crv -> Order,
  133.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crv -> PType);
  134.  
  135.     if (OptiLin && Crv -> Order == 2) {     /* Simply copy the control polygon. */
  136.     CagdRType
  137.         **CrvPoints = Crv -> Points;
  138.  
  139.     for (Count = 0; Count < n && Count < Len; Count++)
  140.         for (i = IsNotRational; i <= MaxCoord; i++)
  141.             Points[i][Count] = CrvPoints[i][Count];
  142.  
  143.     if (Count < n &&
  144.         CAGD_IS_BSPLINE_CRV(Crv) &&
  145.         CAGD_IS_PERIODIC_CRV(Crv)) {
  146.         for (i = IsNotRational; i <= MaxCoord; i++)
  147.             Points[i][Count] = CrvPoints[i][0];
  148.         Count++;
  149.     }
  150.  
  151.     return Count;
  152.     }
  153.  
  154.     if (FineNess > 0) {
  155.     CagdRType *RefKV, Tmin, Tmax;
  156.     int PeriodicLen = CAGD_CRV_PT_LST_LEN(Crv);
  157.  
  158.     /* Make sure we refine some place... */
  159.     if (n <= PeriodicLen)
  160.         CAGD_FATAL_ERROR(CAGD_ERR_REF_LESS_ORIG);
  161.  
  162.     CagdCrvDomain(Crv, &Tmin, &Tmax);
  163.  
  164.     RefKV = BspKnotPrepEquallySpaced(n - PeriodicLen, Tmin, Tmax);
  165.  
  166.     if (CAGD_IS_BEZIER_CRV(Crv)) {
  167.         CagdRType
  168.         *KV = BspKnotUniformOpen(Crv -> Length, Crv -> Order, NULL);
  169.  
  170.         A = BspKnotEvalAlphaCoefMerge(Order, KV, Len, RefKV, n - Len);
  171.  
  172.         IritFree((VoidPtr) KV);
  173.     }
  174.     else {
  175.         A = BspKnotEvalAlphaCoefMerge(Order, Crv -> KnotVector,
  176.                       PeriodicLen, RefKV, n - PeriodicLen);
  177.     }
  178.  
  179.     IritFree((VoidPtr) RefKV);
  180.     }
  181.  
  182.     /* Compute the refined control polygon straight to destination Points. */
  183.     for (j = IsNotRational; j <= MaxCoord; j++) {
  184.     CagdRType
  185.         *ROnePts = Points[j],
  186.         *OnePts = Crv -> Points[j];
  187.     for (i = 0; i < n; i++, ROnePts++)
  188.         CAGD_ALPHA_BLEND(A, i, OnePts, Crv -> Length, ROnePts);
  189.     }
  190.  
  191.     if (FineNess > 0)
  192.     BspKnotFreeAlphaCoef(A);
  193.  
  194.     return n;
  195. }
  196.